<?php

use yii\db\Migration;

/**
 * Class m240928_152632_crop
 */
class m240928_152632_crop extends Migration
{

    const TB_CROP = '{{%crop}}'; // 组织表

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $userOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $userOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB AUTO_INCREMENT=1';
        }

        $this->createTable(self::TB_CROP, [
            'id' => $this->primaryKey()->unsigned()->unique(),
            'country_code' => $this->smallInteger()->unsigned()->notNull()->defaultValue(86)->comment('国家代码'),
            'corp_id' => $this->string(50)->unique()->notNull()->defaultValue('')->comment('组织CropID，在钉钉企业开发后台获取'),
            'corp_name' => $this->string(50)->notNull()->defaultValue('')->comment('组织名称'),
            'api_token' => $this->string(72)->unique()->comment('API Token，在钉钉企业开发后台生成，用于校验宜搭连接器请求'),
            'access_token' => $this->string(64)->unique()->comment('企业内部应用调用接口时的accessToken，通过接口获得，7200秒过期'),
            'status' => $this->boolean()->notNull()->defaultValue(10)->comment('账号状态：-1已注销，0黑名单，10激活'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->notNull(),
        ], $userOptions);
        // 创建手机号为索引
        $this->createIndex('idx-corp_id', self::TB_CROP, ['corp_id', 'country_code', 'status']);
        // 创建昵称为索引
        $this->createIndex('idx-corp_name', self::TB_CROP, ['corp_name', 'status']);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m240928_152632_crop cannot be reverted.\n";

        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m240928_152632_crop cannot be reverted.\n";

        return false;
    }
    */
}
